 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name">
        <div class="name width2">财务报表</div>
      </div>
      <div class="content-con">
        <el-form :model="form" ref="form" class="btn-search" size="medium" label-width="120px">
          <el-form-item label="日期:">
            <el-date-picker
              v-model="form.date"
              type="daterange"
              range-separator="——"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="业务类型:" prop="business_type">
            <el-select v-model="form.business_type" filterable clearable style="width: 100%;">
              <el-option
                v-for="item in businessList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-button class="save mb-22 ml-20" @click="search">查 询</el-button>
        </el-form>

        <div class="bank-form">
          <div class="form-item">
            <el-table
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              class="table"
              stripe border
              show-summary
              :summary-method="getSummaries"
              :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }">
              <el-table-column prop="date" label="日期">
              </el-table-column>
              <el-table-column prop="no" label="单据编号">
              </el-table-column>
              <el-table-column prop="business_type" label="业务类型">
              </el-table-column>
              <el-table-column prop="income" label="收入(元)">
              </el-table-column>
              <el-table-column prop="spend" label="支出(元)">
              </el-table-column>
              <el-table-column prop="note" label="备注">
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页-页码 -->
          <div class="page">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableData.length">
            </el-pagination>
          </div>
        </div>
      </div>
   </el-card>

 </template>
<script>

export default {
  data () {
    return {
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      tableData: [
        {
          date: '2021-03-03',
          no: 'XS202011181609499',
          business_type: '销货',
          income: '1000',
          spend: '',
          note: ''
        },
        {
          date: '2021-03-03',
          no: 'CG202005281048074',
          business_type: '购货',
          income: '',
          spend: '400',
          note: ''
        }
      ],
      businessList: [
        {value: '0',label: '业务类型'},
        {value: '1',label: '购货'},
        {value: '2',label: '销货'}
      ],
      form: {
        date: '',
        business_type: '0'
      }
    }
  },

  methods: {
    //搜索
    search() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },

    //表格合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = []; //声明变量
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计：';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if(
          column.property == "income" || column.property == "spend"
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
        }
      });
      return sums;
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
